package com.example.shudi.sdkdemo.activity;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.example.shudi.sdkdemo.R;
import com.example.shudi.sdkdemo.fragment.DemoFragment;
import com.example.shudi.sdkdemo.fragment.EmotFragment;
import com.example.shudi.sdkdemo.fragment.FaceContrastFragment;
import com.example.shudi.sdkdemo.fragment.WebFragment;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private final String FRAGMENT_TAG = "fragment_tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        getFragmentManager().beginTransaction().replace(R.id.fl_container,
                new DemoFragment()).commitAllowingStateLoss();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG);
            if (fragment == null) {
                super.onBackPressed();
            } else {
                getFragmentManager().beginTransaction().replace(R.id.fl_container,
                        new DemoFragment()).commitAllowingStateLoss();
            }
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        setTitle(item.getTitle());

        if (id == R.id.nav_face) {

            replaceFragment(new EmotFragment());

        }
//        else if (id == R.id.nav_danger) {
//
//            replaceFragment(new DangderFragment());
//
//        }
        else if (id == R.id.nav_compare) {
            replaceFragment(new FaceContrastFragment());
        }
//        else if (id == R.id.nav_real_time) {
//            replaceFragment(new DetectorFragment());
//        }
        else if (id == R.id.nav_web) {
            replaceFragment(new WebFragment());

        } else if (id == R.id.nav_us) {
            replaceFragment(new DemoFragment());
        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private void replaceFragment(Fragment fragment) {
        getFragmentManager().beginTransaction().replace(R.id.fl_container,
                fragment, FRAGMENT_TAG).commitAllowingStateLoss();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG);
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
